// pages/search/search.js
const db = wx.cloud.database()
const hotSearch = db.collection('hotsearch')
const eventSearch = db.collection('event')
const clubSearch = db.collection('club')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hotSearchText:[],
    ifSearch:false,
    eventList:[],
    clubList:[],
    value:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    hotSearch
    .orderBy('count','desc')
    .limit(6)
    .get({
        success(res) {
          _this.setData({
            hotSearchText:res.data
          })
        }
      })
  },

  Gotoclubdetail:function(e){
    console.log(e)
    var that = this
    var app = getApp();
    wx.navigateTo({
      url: '/pages/clubprofile/clubprofile?id=' + e.currentTarget.dataset.id + '&userOpenId=' + app.openID,
    })
  },

  Gotoeventdetail: function (e) {
    wx.navigateTo({
      url: '/pages/eventdetail/eventdetail?id=' + e.currentTarget.dataset.id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onSearch:function(e){
    var _this = this
    var tempclublist =[]
    const _ = db.command
    _this.setData({
      ifSearch: true,
      eventList:[],
      clubList:[]
    })

    hotSearch
      .where({
        searchtext: e.detail
      })
      .get({
        success(res) {
          console.log(res.data.length)
          if(res.data.length===0){
            hotSearch.add({
              data:{
                searchtext: e.detail,
                count: 1
              }
            })
          }else{
            console.log(res.data[0]._id)
            wx.cloud.callFunction({
              name: 'addSearchCount',
              data:{
                _id: res.data[0]._id,
              },
              complete: console.log
            })
          }
        }
      })

    eventSearch
      .where({
        profile: db.RegExp({
          regexp: e.detail
        })
      })
      .get({
        success(res) {
          _this.setData({
            eventList: res.data
          })
        }
      })

    clubSearch
      .where(_.or([
        {
          name: db.RegExp({
            regexp: e.detail
          })
        },
        {
          profile: db.RegExp({
            regexp: e.detail
          })
        }
      ]))
      .get({
        success(res) {
          _this.setData({
            clubList: res.data
          })
        }
      })

  },

  onClear: function (res) {
    var _this = this
    _this.setData({
      ifSearch: false
    })
  },

  onHotSearch: function (res) {
    var _this = this
    _this.setData({
      value: res.currentTarget.dataset.text,
      ifSearch: true,
      eventList: []
    })
    eventSearch
      .where({
        profile: db.RegExp({
          regexp: res.currentTarget.dataset.text
        })
      })
      .get({
        success(res) {
          _this.setData({
            eventList: res.data
          })
        }
      })

    clubSearch
      .where({
        profile: db.RegExp({
          regexp: res.currentTarget.dataset.text
        })
      })
      .get({
        success(res) {
          _this.setData({
            clubList: res.data
          })
        }
      })
  }

})